package com.example.sens.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.example.sens.entity.Quiz;
import com.example.sens.entity.User;
import com.example.sens.entity.UserQuizResult;
import com.example.sens.entity.UserResult;
import com.example.sens.service.MessageService;
import com.example.sens.service.QuizService;
import com.example.sens.service.UserService;
import com.example.sens.utils.Response;
import com.example.sens.vo.QuizVo;
import com.example.sens.vo.UserResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Objects;

/**
 *  实验
 * @date 2020-04-23 13:10
 */

@RestController
@RequestMapping("/api/quiz")
@Slf4j
public class QuizController {

    @Autowired
    private QuizService quizService;

    /**
     * 获取分页数据
     * @param quizVo
     * @return
     */
    @PostMapping("/getByPage")
    public Response<Page<Quiz>> getByPage(@RequestBody QuizVo quizVo) {
        Page<Quiz> quizPage = null;
        try {
            quizPage = quizService.findAll(new Page(quizVo.getPageNo(), quizVo.getPageSize()));
        } catch (Exception e) {
            e.printStackTrace();
            return Response.no();
        }
        return Response.yes(quizPage);
    }

    @PostMapping("/saveQuiz")
    public Response saveQuiz(@RequestBody QuizVo quizVo) {
        try{
            quizService.saveQuiz(quizVo);
        }catch (Exception e){
            log.error("saveQuiz error",e);
            return Response.no(e.getMessage());
        }
        return Response.yes();
    }

    @PostMapping("/getQuestionList")
    public Response<QuizVo> getQuestionList(@RequestParam("quizId") Long quizId){
        QuizVo quizVo = null;
        try{
            quizVo =  quizService.getQuestionList(quizId);
        }catch (Exception e){
            log.error("getQuestionList error",e);
            return Response.no(e.getMessage());
        }
        return Response.yes(quizVo);
    }

    @PostMapping("/saveQuizResult")
    public Response<String> saveQuizResult(@RequestBody QuizVo quizVo) {
        Response<String> response = null;
        try{
            response = quizService.saveQuizResult(quizVo);
        }catch (Exception e){
            log.error("saveQuizResult error",e);
            return Response.no(e.getMessage());
        }
        return Response.yes(response.getData());
    }

    /**
     * 获取分页数据
     * @param resultVo
     * @return
     */
    @PostMapping("/getByPersonPage")
    public Response<Page<UserResult>> getByPage(@RequestBody UserResultVo resultVo,HttpSession session) {
        Page<UserResult> quizPage = null;
        try {
            User user = (User) session.getAttribute("user");
            Long userId  = user.getUserId();
            quizPage = quizService.findPersonalQuiz(new Page(resultVo.getPageNo(), resultVo.getPageSize()),userId);
        } catch (Exception e) {
            e.printStackTrace();
            return Response.no();
        }
        return Response.yes(quizPage);
    }
}
